package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.syaccp.mapper.OrderInfoMapper;
import com.syaccp.model.OrderInfo;
import com.syaccp.service.OrderDataGatheringVoService;
import com.syaccp.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderDataGatheringVoServiceImpl implements OrderDataGatheringVoService {

    @Resource
    OrderInfoMapper orderInfoMapper;

    @Override
    public Integer getTodayOrderManySum() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("COUNT(*) as orderDataGathering")
                .between("create_time", DateUtils.getTodayDateTimeFirst(), DateUtils.getTodayDateTimeLast())
                .notIn("status", 3);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return (int) Math.round(orderInfo.getOrderDataGathering());
    }

    @Override
    public Double getTodayOrderMoneySum() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("IFNULL(SUM(money), 0.0) as orderDataGathering")
                .between("create_time", DateUtils.getTodayDateTimeFirst(), DateUtils.getTodayDateTimeLast())
                .notIn("status", 3);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return orderInfo.getOrderDataGathering();
    }

    @Override
    public Double getMonthOrderMoneySum() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("IFNULL(SUM(money), 0.0) as orderDataGathering")
                .between("create_time", DateUtils.getMonthBeginDateTime(), DateUtils.getTodayDateTimeLast())
                .notIn("status", 3);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return orderInfo.getOrderDataGathering();
    }

    @Override
    public Double getTotalOrderMoneySum() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("SUM(money) as orderDataGathering").notIn("status", 3);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return orderInfo.getOrderDataGathering();
    }

    @Override
    public Integer getOrderTotalCount() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("COUNT(id) as orderDataGathering");
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return (int) Math.round(orderInfo.getOrderDataGathering());
    }

    @Override
    public Integer getPlaceOrderStatusCount() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("COUNT(id) as orderDataGathering").eq("status", 1);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return (int) Math.round(orderInfo.getOrderDataGathering());
    }

    @Override
    public Integer getSuccessOrderStatusCount() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("COUNT(id) as orderDataGathering").eq("status", 2);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return (int) Math.round(orderInfo.getOrderDataGathering());
    }

    @Override
    public Integer getFailOrderStatusCount() {
        QueryWrapper<OrderInfo> qw = Wrappers.query();
        qw.select("COUNT(id) as orderDataGathering").eq("status", 3);
        OrderInfo orderInfo = orderInfoMapper.selectOne(qw);
        return (int) Math.round(orderInfo.getOrderDataGathering());
    }
}
